import java.awt.*;
import java.awt.event.*;

public class DemoWithCommands extends Frame implements ActionListener
{
   Menu mnuFile;
   fileOpenCommand mnuItemOpen;
   fileExitCommand mnuItemExit;
	fileClearCommand mnuItemClear;
   btnRedCommand btnRed;
	btnBlueCommand btnBlue;
	btnYellowCommand btnYellow;
   Panel p;
   Frame fr;
  
   public DemoWithCommands()
   {
     super("Frame with commands");
     fr = this;  //save frame object
		
	  p = new Panel();
     add(p);
     btnRed = new btnRedCommand("Red", p);
	  btnBlue = new btnBlueCommand("Blue", p);
	  btnYellow = new btnYellowCommand("Yellow", p);
      
     p.add(btnRed);
	  p.add(btnYellow);
	  p.add(btnBlue);

     btnRed.addActionListener(this);
	  btnBlue.addActionListener(this);
	  btnYellow.addActionListener(this);
	  
     setBounds(100,100,340,150);
     setVisible(true);
		
     MenuBar mbar = new MenuBar();
     setMenuBar(mbar);

     mnuFile = new Menu("File", true);
     mbar.add(mnuFile);

     mnuItemOpen = new fileOpenCommand("Open...", this);
	  mnuItemClear = new fileClearCommand("Clear",p);
     mnuItemExit = new fileExitCommand("Exit");
	  mnuFile.add(mnuItemOpen);
	  mnuFile.add(mnuItemClear); 
	  mnuFile.add(mnuItemExit);

     mnuItemOpen.addActionListener(this);
	  mnuItemClear.addActionListener(this);
     mnuItemExit.addActionListener(this);
      
  }
	 
  public void actionPerformed(ActionEvent e) {
    Command obj = (Command)e.getSource();
    obj.Execute();
  }
	
  static public void main(String argv[]) {
    DemoWithCommands Foo = new DemoWithCommands();
    ObserverImpl Bar = new ObserverImpl();
    Foo.addObserver(Bar);
  }
}

//=== button commands =======================================

class btnRedCommand extends Button implements Command {
  Panel p;
  public btnRedCommand(String caption, Panel pnl) {
    super(caption);
    p = pnl;
  }
  public void Execute(){ p.setBackground(Color.red); }
}

class btnYellowCommand extends Button implements Command {
  Panel p;
  public btnYellowCommand(String caption, Panel pnl) {
    super(caption);
    p = pnl;
  }
  public void Execute(){ p.setBackground(Color.yellow); }
}

class btnBlueCommand extends Button implements Command {
  Panel p;
  public btnBlueCommand(String caption, Panel pnl) {
    super(caption);
    p = pnl;
  }
  public void Execute(){ p.setBackground(Color.blue); }
}

//=== menu commands =======================================

class fileOpenCommand extends MenuItem implements Command {
  Frame fr;
  public fileOpenCommand(String caption, Frame frm) {
    super(caption);
    fr = frm;
  }
  public void Execute() {
    FileDialog fDlog = new FileDialog(fr,"Open file",FileDialog.LOAD);
    fDlog.show();
  }
}

class fileExitCommand extends MenuItem implements Command {
  public fileExitCommand(String caption) {
    super(caption);
  }
  public void Execute() { System.exit(0); }
}

class fileClearCommand extends MenuItem implements Command {
  Panel p;
  public fileClearCommand(String caption, Panel pnl) {
	 super(caption);
    p = pnl;
  }
  public void Execute() { p.setBackground(Color.white); }
}


